package com.grgbanking.it.common.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

import org.apache.commons.lang.StringUtils;

import com.grgbanking.it.common.HttpConnection;

/**
 * OA信息触发器
 * 
 * OAMessager
 * 
 * @author 潘广伟
 * @Email p_3er@qq.com
 * @Date 2015-1-9 下午1:20:30
 * 
 * @version 1.0.0
 *
 */
public class OAMessager {
	
	private String address = "http://10.252.252.26/interface/sms.php?USER_ID=J4OKCF9C6H7&PASSWORD=JKL3U7PC6E8&SMS_TYPE=0&FROM_ID=admin";
	private static OAMessager common = new OAMessager();
	
	// 单例
	public static OAMessager getInstance() {
		return common;
	}
	
	public  void sent(String userId,String content) throws IOException{
		StringBuffer message = new StringBuffer(address);
		message.append("&TO_ID=").append(userId);
		message.append("&CONTENT=").append(URLEncoder.encode(content, "UTF-8"));
		System.out.println(message);
		URL url = new URL(message.toString());

		HttpURLConnection connection = (HttpURLConnection) url.openConnection();
		connection.connect();

		BufferedReader reader = new BufferedReader(new InputStreamReader(
				connection.getInputStream()));
		String lines; 

        while ((lines = reader.readLine()) != null) { 
            System.out.println(lines); 
        } 

        reader.close(); 

        // 断开连接 
        connection.disconnect(); 
		
		
	}
	
	/**
	 * 当供应商有反馈信息时发送OA信息
	 * sentWhenSupplierFeekback
	 * 
	 * @param userIds
	 * @param oaCode
	 * @throws IOException 
	 * @exception 
	 * @since  1.0.0
	 */
	public void sentWhenSupplierFeekback(String userIds,String oaCode) throws IOException{
		if(StringUtils.isNotEmpty(userIds)){
			StringBuffer content = new StringBuffer(oaCode);
			content.append("流程，供应商已反馈信息!");
			
			String[] arr = userIds.split(",");
			for(String id : arr){
				if(StringUtils.isNotEmpty(id)){
					this.sent(id, content.toString());
				}
			}
		}
	}
}
